#ifndef	BASE_CTENTRYINDEXMANAGER_H_
#define	BASE_CTENTRYINDEXMANAGER_H_
#include <set>
#include <map>
#include "ct_mutex.h"
#include "ctSingleton.h"

template <int dummy>
class EntryIndexManager:public Singleton<EntryIndexManager>
{
        public:
                void addEntryIndex(unsigned int entryid, unsigned int indexid)
                {
                        ctMutex_scope_lock guard(mlock);
                        indexSet[indexid].insert(entryid);
                }
                void removeEntryIndex(unsigned int entryid, unsigned int indexid)
                {
                        ctMutex_scope_lock guard(mlock);
                        indexSet[indexid].erase(entryid);
                }
                const std::set<unsigned int> getEntrySetByIndex(unsigned int indexid)
                {
                        ctMutex_scope_lock guard(mlock);
                        return indexSet[indexid];
                }
                void removeEntrySetByIndex(unsigned int indexid)
                {
                        ctMutex_scope_lock guard(mlock);
                        indexSet.erase(indexid);
                }
                bool execEvery(execEntry<std::pair<const unsigned int, std::set<unsigned int>>> &eee)
                {
                        ctMutex_scope_lock guard(mlock);
                        typedef std::pair<const unsigned int, std::set<unsigned int>> value_type;
                        EntryIndexSet::iterator iter = indexSet.begin();
                        for (; iter != indexSet.end(); ++iter)
                        {
                                if (!eee.exec(static_cast<value_type*>(&(*iter))))
                                {
                                        return false;
                                }
                        }
                        return true;
                }
        private:
                static EntryIndexManager *instance;

                typedef std::set<unsigned int> EntrySet;
                typedef std::map<unsigned int, EntrySet> EntryIndexSet;
                EntryIndexSet indexSet;
                ctMutex mlock;
};

template <int dummy>
EntryIndexManager<dummy> *EntryIndexManager<dummy>::instance = NULL;

#endif //BASE_CTENTRYINDEXMANAGER_H_
